package com.wangdao.wechatmall.service.admin;

import com.wangdao.wechatmall.bean.pojo.Region;
import com.wangdao.wechatmall.bean.pojo.RegionExample;
import com.wangdao.wechatmall.bean.vo.admin.RegionVO;
import com.wangdao.wechatmall.bean.vo.wx.WXProvinceListVO;
import com.wangdao.wechatmall.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author liyongqiang
 * CreateDate 2021/7/8 21:02
 */

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<RegionVO> getList() {

        List<RegionVO> regionVOList = new ArrayList<>();

        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        criteria.andTypeEqualTo((byte) 1).andPidEqualTo(0);

        //省
        List<Region> regions = regionMapper.selectByExample(regionExample);
        for (Region region : regions) {
            RegionVO regionVO = new RegionVO();
            regionVO.setId(region.getId());
            regionVO.setName(region.getName());
            regionVO.setType(1);
            regionVO.setCode(region.getCode());
            List<RegionVO.ChildrenDTO> childrenDTOList = new ArrayList<>();

            RegionExample regionExample2 = new RegionExample();
            RegionExample.Criteria criteria2 = regionExample2.createCriteria();
            criteria2.andTypeEqualTo((byte) 2).andPidEqualTo(region.getId());
            List<Region> region1s = regionMapper.selectByExample(regionExample2);
            for (Region region1 : region1s) {
                RegionVO.ChildrenDTO childrenDTO = new RegionVO.ChildrenDTO();
                childrenDTO.setId(region1.getId());
                childrenDTO.setName(region1.getName());
                childrenDTO.setType(2);
                childrenDTO.setCode(region1.getCode());
                List<RegionVO.ChildrenDTO.Children> childrenList = new ArrayList<>();


                RegionExample regionExample3 = new RegionExample();
                RegionExample.Criteria criteria3 = regionExample3.createCriteria();
                criteria3.andTypeEqualTo((byte) 3).andPidEqualTo(region1.getId());
                List<Region> region2s = regionMapper.selectByExample(regionExample3);
                for (Region region2 : region2s) {
                    RegionVO.ChildrenDTO.Children children = new RegionVO.ChildrenDTO.Children();
                    children.setId(region2.getId());
                    children.setType(3);
                    children.setCode(region2.getCode());
                    children.setName(region2.getName());
                    childrenList.add(children);
                }
                childrenDTO.setChildren(childrenList);
                childrenDTOList.add(childrenDTO);
            }

            regionVO.setChildren(childrenDTOList);
            regionVOList.add(regionVO);
        }
        return regionVOList;
    }

    @Override
    public List<WXProvinceListVO> getProvinceListByPid(Integer pid) {
        // construct criteria
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        criteria.andPidEqualTo(pid);

        // find regions
        List<Region> regions = regionMapper.selectByExample(regionExample);
        Iterator<Region> iterator = regions.iterator();

        // return wxProvinceList
        List<WXProvinceListVO> wxProvinceList = new ArrayList<>();

        while (iterator.hasNext()) {

            // traverse
            Region region = iterator.next();

            // construct  wxProvinceListVO
            WXProvinceListVO wxProvinceListVO = new WXProvinceListVO();

            wxProvinceListVO.setCode(region.getCode());
            wxProvinceListVO.setPid(region.getPid());
            wxProvinceListVO.setType(region.getType());
            wxProvinceListVO.setName(region.getName());
            wxProvinceListVO.setId(region.getId());

            // add wxProvinceListVO to wxProvinceList
            wxProvinceList.add(wxProvinceListVO);
        }

        return wxProvinceList;
    }
}
